package itheima.itheima_247;

import java.util.Comparator;
import java.util.TreeSet;

/*
    比较器排序
    存储学生对象并遍历，创建TreeSet集合使用带参构造方法
    要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母排序排序
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num= s1.getAge()-s2.getAge();
                int num2=num==0?s1.getName().compareTo(s2.getName()):num;
                return num2;

            }
        });
        Student s1 = new Student("xishi", 29);
        Student s2 = new Student("diaochan", 28);
        Student s3 = new Student("wangzhaojun", 30);
        Student s4 = new Student("yangyuhuan", 33);

        Student s5=new Student("linqingxia",33);
        Student s6=new Student("lingqingxia",33);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);


        for (Student s:ts){
            System.out.println(s.getName()+","+s.getAge());
        }

    }


}
